package com.example.wanglong.myoneapplication.activity;

import android.annotation.SuppressLint;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.TabActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.RequiresPermission;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import com.example.wanglong.myoneapplication.R;
import com.example.wanglong.myoneapplication.adbanner.VisaImageCycleView;
import com.example.wanglong.myoneapplication.common.BaseActivity;
import com.example.wanglong.myoneapplication.reparentclass.ReToast;
import com.example.wanglong.myoneapplication.utils.CommonUtil;
import com.example.wanglong.myoneapplication.viewmodel.AdInfo;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.example.wanglong.myoneapplication.adbanner.VisaImageCycleView;

import java.security.PublicKey;
import java.util.ArrayList;

public class BookingActivity extends BaseActivity  implements View.OnClickListener{

    private LinearLayout v;
    private ScrollView mScrollView;
    private RelativeLayout usa;
    VisaImageCycleView imageCycleView;
    private LinearLayout bookingback;
    private LinearLayout detailshare;

    private RelativeLayout contentbooktabs;
    private RelativeLayout contentbooktabs1;

    private TextView topbarone;
    private TextView topbartwo;
    private TextView topbarthree;
    private TextView topbarfour;

    private TextView topbarone1;
    private TextView topbartwo1;
    private TextView topbarthree1;
    private TextView topbarfour1;

    private TextView shoucang;
    private TextView bookingpro;


    private BookingTab02 mTab02;
    private BookingTab01 mTab01;
    private BookingTab03 mTab03;
    private BookingTab04 mTab04;

    /**
     * 底部四个按钮
     */
    private LinearLayout mTabBtnWeixin;
    private LinearLayout mTabBtnFrd;
    private LinearLayout mTabBtnAddress;
    private LinearLayout mTabBtnSettings;

    private LinearLayout mTabBtnWeixin1;
    private LinearLayout mTabBtnFrd1;
    private LinearLayout mTabBtnAddress1;
    private LinearLayout mTabBtnSettings1;


    /**
     * 用于对Fragment进行管理
     */
    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_booking);

        //初始化
        initIndex();
    }

    //初始化
    public void initIndex(){

        v =(LinearLayout) findViewById(R.id.visatitle);
        v.getBackground().setAlpha(0);

        usa=(RelativeLayout) findViewById(R.id.visathree);

        bookingback=(LinearLayout) findViewById(R.id.bookingback);
        detailshare=(LinearLayout) findViewById(R.id.detailshare);

        topbarone=(TextView) findViewById(R.id.topbarone);
        topbartwo=(TextView) findViewById(R.id.topbartwo);
        topbarthree=(TextView) findViewById(R.id.topbarthree);
        topbarfour=(TextView) findViewById(R.id.topbarfour);
        topbarone1=(TextView) findViewById(R.id.topbarone1);
        topbartwo1=(TextView) findViewById(R.id.topbartwo1);
        topbarthree1=(TextView) findViewById(R.id.topbarthree1);
        topbarfour1=(TextView) findViewById(R.id.topbarfour1);

        shoucang=(TextView) findViewById(R.id.shoucang);
        bookingpro=(TextView) findViewById(R.id.bookingpro);

        bookingback.setOnClickListener(this);
        shoucang.setOnClickListener(this);
        bookingpro.setOnClickListener(this);
        detailshare.setOnClickListener(this);


        contentbooktabs = (RelativeLayout) findViewById(R.id.contentbooktabs);
        contentbooktabs1 = (RelativeLayout) findViewById(R.id.contentbooktabs1);



        //滑动title透明度变化
        initChangeTitle();

        initAD();

        initViews();
        fragmentManager = getFragmentManager();
        setTabSelection(0);

    }



    @Override
    public void onClick(View view){
        switch (view.getId()){
            case R.id.bookingback:
                BookingBack();
                break;
            case R.id.id_tab_bottom_weixin:
                setTabSelection(0);
                break;
            case R.id.id_tab_bottom_friend:
                setTabSelection(1);
                break;
            case R.id.id_tab_bottom_contact:
                setTabSelection(2);
                break;
            case R.id.id_tab_bottom_setting:
                setTabSelection(3);
                break;

            case R.id.id_tab_bottom_weixin1:
                setTabSelection(0);
                break;
            case R.id.id_tab_bottom_friend1:
                setTabSelection(1);
                break;
            case R.id.id_tab_bottom_contact1:
                setTabSelection(2);
                break;
            case R.id.id_tab_bottom_setting1:
                setTabSelection(3);
                break;

            case R.id.bookingpro:
                ReToast.showToast("预定成功",BookingActivity.this);
                break;
            case R.id.shoucang:
                ReToast.showToast("收藏成功",BookingActivity.this);
            break;

            case R.id.detailshare:
                BookingShare();
                break;

            default:
                break;
        }
    }

    public void BookingShare(){
        AndroidShare as = new AndroidShare(
                BookingActivity.this,
                "test",
                "http://img6.cache.netease.com/cnews/news2012/img/logo_news.png");
        as.show();
    }

    public void BookingBack(){
        finish();
    }


    private void initViews()
    {

        mTabBtnWeixin = (LinearLayout) findViewById(R.id.id_tab_bottom_weixin);
        mTabBtnFrd = (LinearLayout) findViewById(R.id.id_tab_bottom_friend);
        mTabBtnAddress = (LinearLayout) findViewById(R.id.id_tab_bottom_contact);
        mTabBtnSettings = (LinearLayout) findViewById(R.id.id_tab_bottom_setting);

        mTabBtnWeixin1 = (LinearLayout) findViewById(R.id.id_tab_bottom_weixin1);
        mTabBtnFrd1 = (LinearLayout) findViewById(R.id.id_tab_bottom_friend1);
        mTabBtnAddress1 = (LinearLayout) findViewById(R.id.id_tab_bottom_contact1);
        mTabBtnSettings1 = (LinearLayout) findViewById(R.id.id_tab_bottom_setting1);



        mTabBtnWeixin.setOnClickListener(this);
        mTabBtnFrd.setOnClickListener(this);
        mTabBtnAddress.setOnClickListener(this);
        mTabBtnSettings.setOnClickListener(this);

        mTabBtnWeixin1.setOnClickListener(this);
        mTabBtnFrd1.setOnClickListener(this);
        mTabBtnAddress1.setOnClickListener(this);
        mTabBtnSettings1.setOnClickListener(this);
    }



    /**
     * 根据传入的index参数来设置选中的tab页。
     *
     */
    @SuppressLint("NewApi")
    private void setTabSelection(int index)
    {
        // 重置按钮
        resetBtn();
        // 开启一个Fragment事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        // 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
        hideFragments(transaction);
        switch (index)
        {
            case 0:
                // 当点击了消息tab时，改变控件的图片和文字颜色
                ((TextView) mTabBtnWeixin.findViewById(R.id.btn_tab_bottom_weixin))
                        .setTextColor(Color.rgb(0, 202, 162));
                topbarone.setBackgroundResource(R.color.tab_item_selected_color);

                ((TextView) mTabBtnWeixin1.findViewById(R.id.btn_tab_bottom_weixin1))
                        .setTextColor(Color.rgb(0, 202, 162));
                topbarone1.setBackgroundResource(R.color.tab_item_selected_color);

                if (mTab01 == null)
                {
                    // 如果MessageFragment为空，则创建一个并添加到界面上
                    mTab01 = new BookingTab01();
                    transaction.add(R.id.id_content, mTab01);
                } else
                {
                    // 如果MessageFragment不为空，则直接将它显示出来
                    transaction.show(mTab01);
                }
                break;
            case 1:
                // 当点击了消息tab时，改变控件的图片和文字颜色
                ((TextView) mTabBtnFrd.findViewById(R.id.btn_tab_bottom_friend))
                        .setTextColor(Color.rgb(0, 202, 162));
                topbartwo.setBackgroundResource(R.color.tab_item_selected_color);

                ((TextView) mTabBtnFrd1.findViewById(R.id.btn_tab_bottom_friend1))
                        .setTextColor(Color.rgb(0, 202, 162));
                topbartwo1.setBackgroundResource(R.color.tab_item_selected_color);

                if (mTab02 == null)
                {
                    // 如果MessageFragment为空，则创建一个并添加到界面上
                    mTab02 = new BookingTab02();
                    transaction.add(R.id.id_content, mTab02);
                } else
                {
                    // 如果MessageFragment不为空，则直接将它显示出来
                    transaction.show(mTab02);
                }
                break;
            case 2:
                // 当点击了动态tab时，改变控件的图片和文字颜色
                ((TextView) mTabBtnAddress.findViewById(R.id.btn_tab_bottom_contact))
                        .setTextColor(Color.rgb(0, 202, 162));
                topbarthree.setBackgroundResource(R.color.tab_item_selected_color);

                ((TextView) mTabBtnAddress1.findViewById(R.id.btn_tab_bottom_contact1))
                        .setTextColor(Color.rgb(0, 202, 162));
                topbarthree1.setBackgroundResource(R.color.tab_item_selected_color);

                if (mTab03 == null)
                {
                    // 如果NewsFragment为空，则创建一个并添加到界面上
                    mTab03 = new BookingTab03();
                    transaction.add(R.id.id_content, mTab03);
                } else
                {
                    // 如果NewsFragment不为空，则直接将它显示出来
                    transaction.show(mTab03);
                }
                break;
            case 3:
                // 当点击了设置tab时，改变控件的图片和文字颜色
                ((TextView) mTabBtnSettings.findViewById(R.id.btn_tab_bottom_setting))
                        .setTextColor(Color.rgb(0, 202, 162));
                topbarfour.setBackgroundResource(R.color.tab_item_selected_color);

                ((TextView) mTabBtnSettings1.findViewById(R.id.btn_tab_bottom_setting1))
                        .setTextColor(Color.rgb(0, 202, 162));
                topbarfour1.setBackgroundResource(R.color.tab_item_selected_color);

                if (mTab04 == null)
                {
                    // 如果SettingFragment为空，则创建一个并添加到界面上
                    mTab04 = new BookingTab04();
                    transaction.add(R.id.id_content, mTab04);
                } else
                {
                    // 如果SettingFragment不为空，则直接将它显示出来
                    transaction.show(mTab04);
                }
                break;
        }
        transaction.commit();
    }

    /**
     * 清除掉所有的选中状态。
     */
    private void resetBtn()
    {
        ((TextView) mTabBtnWeixin.findViewById(R.id.btn_tab_bottom_weixin))
                .setTextColor(Color.rgb(0, 0, 0));
        ((TextView) mTabBtnFrd.findViewById(R.id.btn_tab_bottom_friend))
                .setTextColor(Color.rgb(0, 0, 0));
        ((TextView) mTabBtnAddress.findViewById(R.id.btn_tab_bottom_contact))
                .setTextColor(Color.rgb(0, 0, 0));
        ((TextView) mTabBtnSettings.findViewById(R.id.btn_tab_bottom_setting))
                .setTextColor(Color.rgb(0, 0, 0));

        ((TextView) mTabBtnWeixin1.findViewById(R.id.btn_tab_bottom_weixin1))
                .setTextColor(Color.rgb(0, 0, 0));
        ((TextView) mTabBtnFrd1.findViewById(R.id.btn_tab_bottom_friend1))
                .setTextColor(Color.rgb(0, 0, 0));
        ((TextView) mTabBtnAddress1.findViewById(R.id.btn_tab_bottom_contact1))
                .setTextColor(Color.rgb(0, 0, 0));
        ((TextView) mTabBtnSettings1.findViewById(R.id.btn_tab_bottom_setting1))
                .setTextColor(Color.rgb(0, 0, 0));

        topbarone.setBackgroundResource(0);
        topbartwo.setBackgroundResource(0);
        topbarthree.setBackgroundResource(0);
        topbarfour.setBackgroundResource(0);

        topbarone1.setBackgroundResource(0);
        topbartwo1.setBackgroundResource(0);
        topbarthree1.setBackgroundResource(0);
        topbarfour1.setBackgroundResource(0);

    }

    /**
     * 将所有的Fragment都置为隐藏状态。
     *
     * @param transaction
     *            用于对Fragment执行操作的事务
     */
    @SuppressLint("NewApi")
    private void hideFragments(FragmentTransaction transaction)
    {
        if (mTab01 != null)
        {
            transaction.hide(mTab01);
        }
        if (mTab02 != null)
        {
            transaction.hide(mTab02);
        }
        if (mTab03 != null)
        {
            transaction.hide(mTab03);
        }
        if (mTab04 != null)
        {
            transaction.hide(mTab04);
        }

    }




    //滑动title透明度变化
    public void initChangeTitle(){
        mScrollView=(ScrollView) findViewById(R.id.scrollvisacontent);
        mScrollView.setOnTouchListener(new TouchListenerImpl());
        final ViewTreeObserver.OnScrollChangedListener listener = new ViewTreeObserver.OnScrollChangedListener() {
            @Override
            public void onScrollChanged() {
                int sy=mScrollView.getScrollY();
                if(sy>=0 && sy<=255){
                    v.getBackground().setAlpha(sy);
                } else if(sy>255){
                    v.getBackground().setAlpha(255);
                }

                if(sy>=(imageCycleView.getHeight()-contentbooktabs.getHeight())){
                    contentbooktabs1.setVisibility(View.VISIBLE);
                } else {
                    contentbooktabs1.setVisibility(View.GONE);
                }
            }
        };
        ViewTreeObserver observer = mScrollView.getViewTreeObserver();
        observer.addOnScrollChangedListener(listener);
    }

    //监控touch事件title透明度变化
    private class TouchListenerImpl implements View.OnTouchListener {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            //System.out.println(view.getScrollY());
            int sy=view.getScrollY();
            if(sy>=0 && sy<=255){
                v.getBackground().setAlpha(view.getScrollY());
            } else if(sy>255){
                v.getBackground().setAlpha(255);
            }

            if(sy>=(imageCycleView.getHeight()-contentbooktabs.getHeight())){
                contentbooktabs1.setVisibility(View.VISIBLE);
            } else {
                contentbooktabs1.setVisibility(View.GONE);
            }
            return false;
        }

    }


    //初始化首页广告
    public void initAD() {

        // 初始化ImageLoader
        @SuppressWarnings("deprecation")
        DisplayImageOptions options = new DisplayImageOptions.Builder().showStubImage(R.drawable.dsbb) // 设置图片下载期间显示的图片
                .showImageForEmptyUri(R.drawable.dsbb) // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.drawable.dsbb) // 设置图片加载或解码过程中发生错误显示的图片
                .cacheInMemory(true) // 设置下载的图片是否缓存在内存中
                .cacheOnDisc(true) // 设置下载的图片是否缓存在SD卡中
                .build(); // 创建配置过得DisplayImageOption对象

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext()).defaultDisplayImageOptions(options)
                .threadPriority(Thread.NORM_PRIORITY - 2).denyCacheImageMultipleSizesInMemory()
                .discCacheFileNameGenerator(new Md5FileNameGenerator()).tasksProcessingOrder(QueueProcessingType.LIFO).build();
        ImageLoader.getInstance().init(config);


        imageCycleView = (VisaImageCycleView) findViewById(R.id.indexCycleView);
        AdInfo adInfo = new AdInfo();
        ArrayList<String> imageUrlList = new ArrayList<String>();
        ArrayList<String> clickUrlList = new ArrayList<String>();
        ArrayList<String> advertisementPosition = new ArrayList<String>();
        //简析报文
        for (int i = 0; i < 3; i++) {
            switch (i){
                case 0:
                    imageUrlList.add("drawable://"+R.drawable.yinghua750_360);
                    clickUrlList.add("photo"+i);
                    advertisementPosition.add(String.valueOf(i));
                    break;
                case 1:
                    imageUrlList.add("drawable://"+R.drawable.online_visa750_360);
                    clickUrlList.add("photo"+i);
                    advertisementPosition.add(String.valueOf(i));
                    break;
                case 2:
                    imageUrlList.add("drawable://"+R.drawable.taiwan750_360);
                    clickUrlList.add("photo"+i);
                    advertisementPosition.add(String.valueOf(i));
                    break;
            }
        }
        adInfo.ClickUrlList = clickUrlList;
        adInfo.ImageUrlList = imageUrlList;
        adInfo.AdvertisementPositionList = advertisementPosition;

        initCarsuelView(adInfo.ClickUrlList, adInfo.ImageUrlList);


    }

    //初始化轮播图
    public void initCarsuelView(final ArrayList<String> imageDescList, ArrayList<String> urlList) {

        LinearLayout.LayoutParams cParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, CommonUtil.getScreenWidth(BookingActivity.this) * 35 / 65);
        imageCycleView.setLayoutParams(cParams);



        VisaImageCycleView.ImageCycleViewListener mAdCycleViewListener = new VisaImageCycleView.ImageCycleViewListener() {
            @Override
            public void onImageClick(int position, View imageView) {
                /**实现点击事件*/
                String url = imageDescList.get(position);

                Toast.makeText(BookingActivity.this, url, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void displayImage(String imageURL, ImageView imageView) {
                ImageLoader.getInstance().displayImage(imageURL, imageView);
            }
        };
        /**设置数据*/
        imageCycleView.setImageResources(imageDescList, urlList, mAdCycleViewListener);
        imageCycleView.startImageCycle();
    }


}
